package com.example.constellation.starfrag;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.constellation.R;
import com.example.constellation.bean.StarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link StarFragment#newInstance} factory method to
 * create an instance of this fragment.
 */


/*
* 星座Fragment
* 包括ViewPager和GridView
* */
public class StarFragment extends Fragment {


    ViewPager starVp;
    GridView starGv;
    LinearLayout pointLayout;
    private List<StarBean.StarinfoBean> mDatas;
    private StarBaseAdapter starBaseAdapter;
    //声明图片数组
    int []imgIds={R.mipmap.pic_starimage,R.mipmap.pic_star};
    //声明ViewPager的数据源
    List<ImageView> ivList;
    //声明管理指示器小圆点的集合
    List<ImageView> pointList;

    //完成定时装置 实现自动滑动效果
    Handler handler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1)
            {
                //获取当前viewpager显示的页面
                int currentItem = starVp.getCurrentItem();
                //判断是否为最后一张 如果是最后一张返回第一张，否则显示最后一张
                if(currentItem== ivList.size()-1)
                {
                    starVp.setCurrentItem(0);
                }
                else
                {
                    currentItem++;
                    starVp.setCurrentItem(currentItem);
                }
                handler.sendEmptyMessageDelayed(1,5000);

            }

        }
    };

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public StarFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment StarFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static StarFragment newInstance(String param1, String param2) {
        StarFragment fragment = new StarFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_star,container,false);
        initView(view);
        Bundle bundle=getArguments();
        StarBean infoBean= (StarBean) bundle.getSerializable("info");
        mDatas=infoBean.getStarinfo();
         starBaseAdapter = new StarBaseAdapter(getContext(), mDatas);
         starGv.setAdapter(starBaseAdapter);
         initPager();
         setVPListener();
         setGVListener();
         //延迟5秒钟发送一条消息
        handler.sendEmptyMessageDelayed(1,5000);





        return view;
    }


    /*设置GridView的监听事件函数*/
    private void setGVListener() {
        starGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StarBean.StarinfoBean starinfoBean = mDatas.get(i);
                Intent intent = new Intent(getContext(), StarAnalysisActivity.class);
                intent.putExtra("star",starinfoBean);
                startActivity(intent);
            }
        });
    }


    /*设置viewpager改变的监听器函数*/
    private void setVPListener() {
        starVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position) {
                for(int i=0;i<pointList.size();i++)
                {
                    pointList.get(i).setImageResource(R.mipmap.point_normal);
                }
                pointList.get(position).setImageResource(R.mipmap.point_focus);
            }
        });

    }

    /*设置ViewPager显示的页面*/
    private void initPager() {
        ivList=new ArrayList<>();
        pointList=new ArrayList<>();
        for(int i=0;i<imgIds.length;i++)
        {
            ImageView iv=new ImageView(getContext());
            iv.setImageResource(imgIds[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            //设置图片的宽高
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(lp);
            //将图片view加载到集合当中
            ivList.add(iv);
            //创建图片对应的指示器小圆点
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(R.mipmap.point_normal);
            LinearLayout.LayoutParams plp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            plp.setMargins(20,0,0,0);
            imageView.setLayoutParams(plp);
            pointLayout.addView(imageView);
            pointList.add(imageView);



        }
        //默认第一个小圆点是获取焦点的状态
        pointList.get(0).setImageResource(R.mipmap.point_focus);
        StarPagerAdapter starPagerAdapter = new StarPagerAdapter(getContext(), ivList);
        starVp.setAdapter(starPagerAdapter);
    }

    /*
    初始化控件操作
     */
    private void initView(View view)
    {
        starVp=view.findViewById(R.id.starfrag_vp);
        starGv=view.findViewById(R.id.starfrag_gv);
        pointLayout=view.findViewById(R.id.starfrag_layout);
    }

}